/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.report;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

import com.bluestone.core.IAction;

/**
 * ActionInvocationHandler implement the InvocationHandler which is the interface implemented by the invocation handler of a proxy instance.
 * Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.
 * @author <a href="mailto:ming7655@gmail.com">Aaron</a>
 * 
 */
public class ActionInvocationHandler implements InvocationHandler {
	private IAction action;

	private ActionProxy actionProxy;

	public ActionInvocationHandler(IAction action, ActionProxy proxy) {
		this.action = action;
		this.actionProxy = proxy;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
	 *      java.lang.reflect.Method, java.lang.Object[])
	 */
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		if (IAction.class.isInstance(proxy)
				&& "execute".equalsIgnoreCase(method.getName())) {
			ArrayList actionHandlers = this.actionProxy.getActionHandlers();
			doActionHandler(actionHandlers, IActionHandler.PREP);
			Object result = method.invoke(action, args);
			doActionHandler(actionHandlers, IActionHandler.POST);
			this.actionProxy.getCurrentReportBean().setActionExecuteResult(String.valueOf(result));
			this.doExpAttributeHandler(actionHandlers, this.actionProxy
					.getCurrentReportBean());
			return result;
		}
		return null;
	}

	private void doActionHandler(ArrayList actionHandlers, String type) {
		if (actionHandlers == null) {
			return;
		}
		for (int i = 0; i < actionHandlers.size(); i++) {
			IActionHandler actionHandler = (IActionHandler) actionHandlers
					.get(i);
			String temp = actionHandler.getType();
			if (IActionHandler.PREP.equals(type) && temp.indexOf(type) >= 0) {
				actionHandler.process(action);
			} else if (IActionHandler.POST.equals(type)
					&& temp.indexOf(type) >= 0) {
				actionHandler.process(action);
			}
		}
	}

	private void doExpAttributeHandler(ArrayList actionHandlers, ActionReportBean rb) {
		if (actionHandlers == null) {
			return;
		}
		for (int i = 0; i < actionHandlers.size(); i++) {
			IActionHandler actionHandler = (IActionHandler) actionHandlers
					.get(i);
			HashMap map = actionHandler.handlerExpAttributes(rb,null);
			if(map==null||map.isEmpty()){
				continue;
			}
			rb.putExpAttributes(map);
		}
	}

}
